#include "common.h"
#include "playstate.h"

#include "titlestate.h"

#ifdef _DEBUG
#define DEBUG_NEW new( _NORMAL_BLOCK, __FILE__, __LINE__ )
#define new DEBUG_NEW
#endif

TitleState::TitleState()
:	GameState( GM_TITLE )
,	_playButton( NULL )
,	_tutorialButton( NULL )
{
}

void TitleState::ChangeState( GameState** currentState, eGameMode nextState )
{
	if( (*currentState)->GetCurrentState() == nextState )
	{
		return;
	}

	if( nextState == GM_PLAY )
	{
		( *currentState )->Release();
		*currentState = new PlayState();

		assert( *currentState != NULL );
		( *currentState )->Initialize();
	}

	if( nextState == GM_TUTORIAL )
	{
		// TODO : Implement tutorial mode
		Core::GetInstance().PrepareToQuit();

		//*currentState = new TutorialState();

		assert( *currentState != NULL );
		//( *currentState )->Initialize();
	}

	return;
}

bool TitleState::Initialize( void* info )
{
	WCHAR str[100];

	int posX = 0;
	int posY = 0;
	int sizeX = GetPrivateProfileInt( L"Title", L"TutorialButtonSizeX", 0, gGameSettingsFilePath );
	int sizeY = GetPrivateProfileInt( L"Title", L"TutorialButtonSizeY", 0, gGameSettingsFilePath );
	GetPrivateProfileString( L"Title", L"TutorialImageFilePath", L"", str, 100, gGameSettingsFilePath );
	_tutorialButton = new Button( posX, posY, sizeX, sizeY );
	_tutorialButton->SetImage( str );

	posX = 0;
	posY = 42;
	sizeX = GetPrivateProfileInt( L"Title", L"PlayButtonSizeX", 0, gGameSettingsFilePath );
	sizeY = GetPrivateProfileInt( L"Title", L"PlayButtonSizeY", 0, gGameSettingsFilePath );
	GetPrivateProfileString( L"Title", L"PlayImageFilePath", L"", str, 100, gGameSettingsFilePath );
	_playButton = new Button( posX, posY, sizeX, sizeY );
	_playButton->SetImage( str );

	_notifier = new UIEventDispatcher();
	_notifier->Attach( _playButton );
	_notifier->Attach( _tutorialButton );

	return true;
}

LRESULT TitleState::MessageHandler( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
	static EventDesc e = {0};
	switch( msg )
	{
	case WM_DESTROY :
		Core::GetInstance().PrepareToQuit();
		break;
	case WM_MOUSEMOVE :
	case WM_LBUTTONDOWN :
	case WM_LBUTTONUP :
		e.eventType		= EVENT_MOUSE;
		e.buttonFlag	= MOUSE_MOVE;
		e.buttonFlag	= ( msg == WM_MOUSEMOVE )?		MOUSE_MOVE :
						  ( msg == WM_LBUTTONDOWN )?	MOUSE_LDOWN :
														MOUSE_LUP ;
		e.curPosX		= LOWORD( lParam );
		e.curPosY		= HIWORD( lParam );
		_notifier->Notify( &e );
		break;
	}
	return DefWindowProc( hWnd, msg, wParam, lParam );
}

void TitleState::Run( GameState** currentState, float time )
{
	LPDIRECT3DDEVICE9 device = Core::GetInstance().GetD3dDevice();

	device->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xFF7F7F7F, 1.0f, 0 );
	
	if( SUCCEEDED( device->BeginScene() ) )
	{
		DrawUI();

		device->EndScene();
	}

	device->Present( NULL, NULL, NULL, NULL );

	if( _playButton->IsButtonClicked() )
	{
		ChangeState( currentState, GM_PLAY );
		return;
	}
	if( _tutorialButton->IsButtonClicked() )
	{
		ChangeState( currentState, GM_TUTORIAL );
		return;
	}
}

void TitleState::Release()
{
	if( _notifier )
	{
		_notifier->DetachAll();
		delete _notifier;
		_notifier = NULL;
	}

	if( _tutorialButton )
	{
		delete _tutorialButton;
		_tutorialButton = NULL;
	}
	if( _playButton )
	{
		delete _playButton;
		_playButton = NULL;
	}
	delete this;
}

void TitleState::DrawUI()
{
	_tutorialButton->Draw();
	_playButton->Draw();
}